package ttOrder.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ttOrder.model.Food;

@Repository
public class FoodDAOImpl extends BaseHibernateDAO<Food> implements IFoodDAO {
	@Autowired
	private SessionFactory sessionFactory;

	public Session getSession() {
		return this.sessionFactory.getCurrentSession();
	}

	@Override
	public Integer deleteFood(Integer id) throws Exception {
		Session session = getSession();
		Food food = (Food) session.get(Food.class, id);

		if (food != null) {
			session.delete(food);

		}
		return null;
	}

	@Override
	public void editFood(Food food) throws Exception {
		this.update(food);

	}

	@Override
	public void createFood(Food food) throws Exception {
		this.save(food);

	}

	@Override
	public Food getFoodById(Integer id) {
		Session session = getSession();
		return (Food) session.get(Food.class, id);
	}

	@Override
	public List<Food> findAllFood() {
		Session session = getSession();
		String hql = "from Food";
		@SuppressWarnings("unchecked")
		List<Food> food = session.createQuery(hql).list();
		return food;
	}
	
	@Override
	public List<Food> findAllFoodByFoodType(Integer id) {
		Session session = getSession();
		String hql = "from Food where FOODTYPE_ID = "+id;
		@SuppressWarnings("unchecked")
		List<Food> food = session.createQuery(hql).list();
		return food;
	}

}
